package com.zhang.webadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.webadmin.pojo.AclUser;
import com.zhang.webadmin.service.AclUserService;
import com.zhang.webadmin.util.R;
import com.zhang.webadmin.vo.EditUserRoleVo;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/acluser")
@Validated
public class AclUserController {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private AclUserService aclUserService;


    @PreAuthorize("hasAuthority('acluser:query')")
    @GetMapping("/list")
    public R list(@RequestParam(required = false, defaultValue = "1") Integer page,
                  @RequestParam(required = false, defaultValue = "10") Integer size) {

        Page<AclUser> dataPage = aclUserService.lambdaQuery()
                .orderByDesc(AclUser::getCreateTime)
                .page(Page.of(page, size));

        return R.page(dataPage.getRecords(), dataPage.getTotal());
    }

    @PreAuthorize("hasAuthority('acluser:query')")
    @GetMapping("/{id}")
    public R getById(@NotNull(message = "id不能为空") @PathVariable Long id) {
        AclUser aclUser = aclUserService.getById(id);
        if (aclUser == null) {
            return R.fail("不存在");
        }
        return R.ok(aclUser);
    }

    @PreAuthorize("hasAuthority('acluser:add')")
    @PostMapping("/add")
    public R add(@Validated AclUser addVo) {
        AclUser insertUser = new AclUser();
        insertUser.setLoginName(addVo.getLoginName());
        insertUser.setLoginPassword(passwordEncoder.encode(addVo.getLoginPassword()));
        insertUser.setShowName(addVo.getShowName());
        insertUser.setStatus(addVo.getStatus());
        insertUser.setCreateTime(LocalDateTime.now());
        insertUser.setUpdateTime(LocalDateTime.now());
        aclUserService.save(insertUser);

        return R.ok();
    }


    @GetMapping("/hasRole")
    public R hasRole(Long userId) {
        if (userId == null) {
            return R.fail("参数错误");
        }
        List<Long> roleIdList = aclUserService.hasRole(userId);
        return R.ok(roleIdList);
    }


    // 分配角色
    @PostMapping("/setRole")
    public R setRole(EditUserRoleVo editUserRoleVo) {
        aclUserService.setRole(editUserRoleVo.getUserId(), editUserRoleVo.getRoleIdList());

        return R.ok();
    }


    @PreAuthorize("hasAuthority('acluser:delete')")
    @DeleteMapping("/{id}")
    public R delete(@NotNull(message = "id不能为空") @PathVariable Long id) {
        AclUser aclUser = aclUserService.getById(id);
        if (aclUser == null) {
            return R.fail("不存在");
        }
        aclUserService.removeById(id);
        return R.ok();
    }


}
